package cn.swu.ws.user;

import cn.swu.ws.dao.MysqlDbDao;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet(name="UserServlet",urlPatterns = "/UserServlet")
public class UserServlet extends HttpServlet {
    public static  String user_name = "";
    public static  boolean login_state = false;
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws RuntimeException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        // 获取用户输入的账户名和密码
        String username = req.getParameter("username");
        user_name = username;
        String password = req.getParameter("password");
        // 获取验证码
        String captcha = req.getParameter("captcha");
        // 验证用户输入的验证码是否正确
        System.out.println(password);
        System.out.println(username);
        System.out.println(captcha);
        if (!captcha.equals(DecodeServlet.decode)) {
            System.out.println("验证码错误");
            String json = "{\n" +
                    "\t\"state\": 200,\n" +
                    "\t\"captcha\": false,\n" +
                    "\t\"msg\": \" 验证码是错误的\",\n" +
                    "\t\"data\": null\n" +
                    "}";
            resp.setContentType("application/json");
            resp.setCharacterEncoding("UTF-8");
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            out.close();
            return;
        }
        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            Connection connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            Connection connection = MysqlDbDao.login_database();
            Statement statement = connection.createStatement();
            String sql = "select username,password from user_information;";
            ResultSet rs = statement.executeQuery(sql);
            System.out.println(rs);
            while(rs.next()){
                // 通过字段检索
                String db_name = rs.getString("username");
                String db_password = rs.getString("password");
                System.out.println(db_name);
                //System.out.println(db_password);
                if(db_name.equals(username) && db_password.equals(password)){
                    HttpSession session = req.getSession(true);
                    login_state = true;
                    //没有数据库的要先创建数据库
                    String create_table_sql_tem = "CREATE TABLE IF NOT EXISTS `%s_book`(\n" +
                            "        `id` VARCHAR(500),\n" +
                            "        `name` VARCHAR(500),\n" +
                            "        `author` VARCHAR(100),\n" +
                            "        `price` FLOAT,\n" +
                            "        `content` VARCHAR(100))";
                    System.out.println(String.format(create_table_sql_tem, username));
                    String create_table_sql = String.format(create_table_sql_tem, username);
                    int resultSet = statement.executeUpdate(create_table_sql);
                    String json = "{\n" +
                            "\t\"state\": 200,\n" +
                            "\t\"captcha\": true,\n" +
                            "\t\"login_state\": true,\n" +
                            "\t\"create_table\": %d\n" +
                            "}";
                    PrintWriter out = resp.getWriter();
                    out.print(String.format(json,resultSet));
                    out.flush();
                    out.close();
                    return;
                }

            }
            String json = "{\n" +
                    "\t\"state\": 200,\n" +
                    "\t\"captcha\": true,\n" +
                    "\t\"login_state\": false,\n" +
                    "\t\"data\": null\n" +
                    "}";
            resp.setContentType("application/json");
            resp.setCharacterEncoding("UTF-8");
            PrintWriter out = resp.getWriter();
            out.print(json);
            out.flush();
            out.close();
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("数据库没有连接");
            System.out.println(e);
            throw new RuntimeException(e);

        }

    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req,resp);
    }

}

//    CREATE TABLE IF NOT EXISTS `admin2_book`(
//        `name` VARCHAR(100),
//        `author` VARCHAR(100),
//        `price` FLOAT,
//        `content` VARCHAR(100),
//        )
